//
//  HMHTTPClient.swift
//  HMWeiBo
//
//  Created by Apple on 16/7/29.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import AFNetworking

enum HMHTTPClientType: String {
    
    case GET = "GET"
    case POST = "POST"
    
}

class HMHTTPClient: AFHTTPSessionManager {

    // 后期会再封装,以及会再说编程思想
    
    // swift里的单例
    static let shareInstance: HMHTTPClient = {
    
        let afn = HMHTTPClient()
        
        // 设置网络请求的 content-type: text/plain
        
        afn.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return afn
    }()
    
    // GET 和 POST,我们需要加一个参数,来区分get和post
    // 可以添加一个bool; 枚举
    func  request(type: HMHTTPClientType,URLString: String, parameters: AnyObject?, progress downloadProgress: ((NSProgress) -> Void)?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?){
    
        if type == .GET {
            
            self.GET(URLString, parameters: parameters, progress: downloadProgress, success: success, failure: failure)
            
        } else  if type == .POST {
            
            self.POST(URLString, parameters: parameters, progress: downloadProgress, success: success, failure: failure)
        }
        
    }
    
    /*
        1.progress downloadProgress: ((NSProgress) -> Void)? 没用
        2.成功或者失败的回调的 NSURLSessionDataTask 没用
        3. 返回的数据是 AnyObject?,我们需要自己处理
    */
    
    func request(type: HMHTTPClientType,URLString: String, parameters: AnyObject?,success: (jsonDict: [String: AnyObject])->(),failure: (error: NSError)->()){
        
        // 网络请求成功之后的回调
        // <#T##((NSURLSessionDataTask, AnyObject?) -> Void)?##((NSURLSessionDataTask, AnyObject?) -> Void)?##(NSURLSessionDataTask, AnyObject?) -> Void#>
       
        let orginalSuccess =  { (task:NSURLSessionDataTask, JSON:AnyObject?) -> Void in
            
            // 网络请求返回之后,我们对数据进行处理
            if let json = JSON as? [String: AnyObject] {
                
                // 如果有数据,我们该
                success(jsonDict: json)
            }
            
        }
        
        
        // 网络请求失败的回调
        // <#T##((NSURLSessionDataTask?, NSError) -> Void)?##((NSURLSessionDataTask?, NSError) -> Void)?##(NSURLSessionDataTask?, NSError) -> Void#>
        
        
        let orginalFailure = { (task:NSURLSessionDataTask?, error:NSError) -> Void in
            
            failure(error: error)
            
            
        }
        
        request(type, URLString: URLString, parameters: parameters, progress: nil, success: orginalSuccess, failure: orginalFailure)
        
        
        
    }
    
    
    // 创建一个类方法 -- 类方法的优越性在这里体现的不太明显,如果接受不了,就算了
    class func request(type: HMHTTPClientType,URLString: String, parameters: AnyObject?,success: (jsonDict: [String: AnyObject])->(),failure: (error: NSError)->()){
        
        
        // 类 需要借助于调用 对象方法
        HMHTTPClient.shareInstance.request(type, URLString: URLString, parameters: parameters, success: success, failure: failure)
    }
    
    
}
